package com.htht.job.executor.service.parameter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.htht.job.executor.model.tree.TreeNode;
import com.htht.job.executor.util.specification.SimpleSpecificationBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jeesys.common.jpa.dao.string.BaseDao;
import org.jeesys.common.jpa.search.DatagridPage;
import org.jeesys.common.jpa.search.Specifications;
import org.jeesys.common.jpa.search.Specifications.Builder;
import org.jeesys.common.jpa.search.specifications.AbstractSpecification;
import org.jeesys.common.jpa.search.specifications.InSpecification;
import org.jeesys.common.jpa.service.string.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.htht.job.core.api.parameter.ParameterService;
import com.htht.job.core.biz.model.ReturnT;
import com.htht.job.executor.dao.parameter.ParameterDao;
import com.htht.job.executor.model.parameter.ParameterModel;

@Transactional
@Service("parameterService")
public class ParameterServiceImpl extends BaseService<ParameterModel> implements
		ParameterService {
	@Autowired
	private ParameterDao parameterDao;

	@Override
	public BaseDao<ParameterModel> getBaseDao() {
		// TODO Auto-generated method stub
		return parameterDao;
	}

	public ParameterModel saveParameter(ParameterModel parameterModel) {
		ParameterModel newparameterModel = new ParameterModel();
		if (!StringUtils.isEmpty(parameterModel.getId())) {
			ParameterModel yparameterModel = this.getById(parameterModel
					.getId());
			yparameterModel.setUpdateTime(new Date());
			yparameterModel.setModelName(parameterModel.getModelName());
			yparameterModel.setModelIdentify(parameterModel.getModelIdentify());
			yparameterModel.setFixedParameter(parameterModel
					.getFixedParameter());
			yparameterModel.setDynamicParameter(parameterModel
					.getDynamicParameter());
            yparameterModel.setType(parameterModel.getType());
            yparameterModel.setUrl(parameterModel.getUrl());
			newparameterModel = this.save(yparameterModel);
		} else {
			parameterModel.setCreateTime(new Date());
			newparameterModel = this.save(parameterModel);
		}
		return newparameterModel;
	}

	public Map<String, Object> pageList(int start, int length,
			ParameterModel parameterModel) {
		Page<ParameterModel> page = this.findPageParameter(start, length,
				parameterModel);
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("recordsTotal", page.getTotalElements()); // 总记录数
		maps.put("recordsFiltered", page.getTotalElements()); // 过滤后的总记录数
		maps.put("data", page.getContent()); // 分页列表
		return maps;
	}

	public Page<ParameterModel> findPageParameter(int start, int length,
			ParameterModel parameterModel) {
		Sort sort = new Sort(Sort.Direction.DESC, "createTime");
		PageRequest d = new PageRequest(start, length, sort);
		Page<ParameterModel> page = this.getPage(
				getWhereClause(parameterModel), d);
		return page;
	}

	public List<ParameterModel> findListParameter() {
		SimpleSpecificationBuilder<ParameterModel> specification=new SimpleSpecificationBuilder();
		specification.add("modelIdentify","notEqual","").add("modelIdentify","isNotNull","");

		List<ParameterModel> list = this.getAll(specification.generateSpecification());
		return list;
	}

	public ReturnT<String> deleteParameter(String id) {
		try {
			this.delete(id);
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			 e.printStackTrace();
		}
		return ReturnT.FAIL;
	}

	public ParameterModel findParameterById(String id) {
		return this.getById(id);
	}

	/**
	 * 动态生成where语句
	 *
	 * @param
	 * @param
	 * @return
	 */
	private Specification<ParameterModel> getWhereClause(
			ParameterModel parameterModel) {
		SimpleSpecificationBuilder<ParameterModel> specification=new SimpleSpecificationBuilder();
		if (!StringUtils.isEmpty(parameterModel.getModelName())) {
			specification.add("modelName","likeAll",parameterModel.getModelName());
		}
		if (!StringUtils.isEmpty(parameterModel.getModelIdentify())) {
			specification.add("modelIdentify","likeAll",parameterModel.getModelIdentify());
		}
		if (!StringUtils.isEmpty(parameterModel.getTreeId())) {
			specification.add("treeId","eq",parameterModel.getTreeId());
		}
		return specification.generateSpecification();
	}

}
